#!/usr/bin/env python
# coding=utf-8
# __author__ = 'Yunchao Ling'

from pymongo import MongoClient
import sys

def checkJCRmiss():
    SERVER = 'localhost'
    PORT = 27017
    DB_NAME = "journal"
    COLLECTION_NAME = "journal"

    connection = MongoClient(SERVER, PORT)
    db = connection[DB_NAME]
    collection = db[COLLECTION_NAME]

    total_count = 0
    jcr_miss = 0
    jcr_unique = 0
    jcr_multi = 0

    infile = open(sys.argv[1], "r")
    outfile1=open(sys.argv[1]+".jcr_title_miss", "w")
    outfile2=open(sys.argv[1]+".jcr_title_unique", "w")
    outfile3=open(sys.argv[1]+".jcr_title_multi", "w")
    for line in infile:
        total_count += 1
        line = line.rstrip()
        splitline = line.split("\t")
        FullTitle = splitline[0].strip()
        ISOAbbr = splitline[1].strip()
        ors = []
        if FullTitle != "":
            ors.append({"FullTitle": {"$regex": "^" + FullTitle + "$", "$options": "$i"}})
        if ISOAbbr != "":
            ors.append({"ISOAbbr": {"$regex": "^" + ISOAbbr + "$", "$options": "$i"}})
            ors.append({"MedAbbr": {"$regex": "^" + ISOAbbr + "$", "$options": "$i"}})
        results = collection.find({"$or": ors}).count()
        if results == 0:
            jcr_miss += 1
            outfile1.write(line+"\n")
            outfile1.flush()
        elif results == 1:
            jcr_unique += 1
            outfile2.write(line+"\n")
            outfile2.flush()
        elif results > 1:
            jcr_multi += 1
            outfile3.write(line+"\n")
            outfile3.flush()
    infile.close()
    outfile1.close()
    outfile2.close()
    outfile3.close()
    connection.close()
    print "TOTAL: " + str(total_count)
    print "JCR TITLE MISS: " + str(jcr_miss)
    print "JCR TITLE UNIQUE: " + str(jcr_unique)
    print "JCR TITLE MULTI: " + str(jcr_multi)

def checkJCR():
    # SERVER = 'localhost'
    SERVER = '10.10.31.13'
    PORT = 27017
    DB_NAME = "journal"
    COLLECTION_NAME = "journal"

    connection = MongoClient(SERVER, PORT)
    db = connection[DB_NAME]
    collection = db[COLLECTION_NAME]

    total_count = 0
    jcr_miss = 0
    jcr_unique = 0
    jcr_multi = 0

    infile = open(sys.argv[1], "r")
    # outfile1=open(sys.argv[1]+".jcrmiss", "w")
    # outfile2=open(sys.argv[1]+".jcrunique", "w")
    # outfile3=open(sys.argv[1]+".jcrmulti", "w")
    infile.readline()
    for line in infile:
        total_count += 1
        line = line.rstrip()
        splitline = line.split("\t")
        issn = splitline[3]
        # ors = []
        # if issn != "":
        #     ors.append({"eISSN": issn})
        #     ors.append({"pISSN": issn})
        # results = collection.find({"$or": ors}).count()
        # if results == 0:
        #     jcr_miss += 1
        #     # outfile1.write(line+"\n")
        #     # outfile1.flush()
        # elif results == 1:
        #     jcr_unique += 1
        #     # outfile2.write(line+"\n")
        #     # outfile2.flush()
        # elif results > 1:
        #     jcr_multi += 1
        #     # outfile3.write(line+"\n")
        #     # outfile3.flush()
        if issn != "":
            results = collection.find({"eISSN": issn}).count()
            if results == 0:
                jcr_miss += 1
            elif results == 1:
                jcr_unique += 1
            elif results > 1:
                jcr_multi += 1
    infile.close()
    # outfile1.close()
    # outfile2.close()
    # outfile3.close()
    connection.close()
    print "TOTAL: " + str(total_count)
    print "JCR MISS: " + str(jcr_miss)
    print "JCR UNIQUE: " + str(jcr_unique)
    print "JCR MULTI: " + str(jcr_multi)

if __name__ == "__main__":
    checkJCRmiss()
